package ru.ssau.customerManagement;

import ru.ssau.customerManagement.controller.Controller;
import ru.ssau.customerManagement.controller.remoting.ClientController;
import ru.ssau.customerManagement.controller.remoting.notifications.ClientSideNotificationController;
import ru.ssau.customerManagement.controller.remoting.notifications.ClientSideNotificationControllerImpl;
import ru.ssau.customerManagement.controller.threads.client.NotificationsListener;
import ru.ssau.customerManagement.view.CustomerManagementForm;

import javax.swing.*;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * Author: illabefat
 * Time: 02.11.2014
 */
public class ClientMain {
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        initLookAndFeel();

        try
        {
            ServerSocket serverSocket = new ServerSocket(4444);
            Socket server = new Socket("localhost", 5555);
            Socket notificationSocket = serverSocket.accept();

            Controller controller = new ClientController(server);
            ClientSideNotificationController notificationController = new ClientSideNotificationControllerImpl();

            final CustomerManagementForm form = new CustomerManagementForm(controller);
            notificationController.registerListener(form);

            Thread notificationListener = new Thread(new NotificationsListener(notificationSocket, notificationController));
            notificationListener.start();

            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    form.setVisible(true);
                }
            });


        } catch (IOException e) {
            JOptionPane.showMessageDialog(null, "Some error during initialization");
            e.printStackTrace();
        }
    }


    private static void initLookAndFeel()
    {
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Windows".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(CustomerManagementForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(CustomerManagementForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(CustomerManagementForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(CustomerManagementForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
    }
}
